<template>
	<div class="wrap" v-loading="loading" element-loading-text="等下,我正在请求" element-loading-spinner="el-icon-loading">
		<basic-opt :has_left="false">
			<template #add>
				<div class="opt-item">
					<el-button @click="addForm" icon="el-icon-circle-plus-outline" type="primary" size="medium">添加固件
					</el-button>
				</div>
			</template>
		</basic-opt>
		
		<div class="table-box">
			<el-table :data="data" stripe :header-cell-style="header_cell_style" :max-height="table_max_height">
				<el-table-column prop="id" label="id"></el-table-column>
				<el-table-column prop="name" label="固件名称"></el-table-column>
				<el-table-column prop="description" label="固件描述"></el-table-column>
				<el-table-column prop="created_at" label="创建日期"></el-table-column>
				<el-table-column fixed="right" label="操作" width="400">
					<template #default="scope">
						<el-button @click="onFirmwareVersion(scope.row)" type="info" plain size="mini">查看固件版本
						</el-button>
						<el-button @click="onEditData(scope.row)" type="success" plain size="mini">编辑</el-button>
						<el-button @click="onDelData(scope.row)" type="danger" plain size="mini">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<page-component @pageChanged="onPageChanged"></page-component>

		<compoment-drawer :drawer_name="drawer_name" :drawer_form="drawer_form" @getDrawerForm="getDrawerForm">
			<el-form ref="form" :model="form" label-width="120px">
				<el-scrollbar height="80vh" v-loading="form_loading" element-loading-text="正在进行请求,请稍后"
					element-loading-spinner="el-icon-loading">
					<div class="form-flex">
						<el-form-item label="固件名称" prop="name">
							<el-input class="w-ipt" v-model="form.name" placeholder="请输入固件名称"></el-input>
						</el-form-item>
						<el-form-item label="固件描述" prop="description">
							<el-input class="w-ipt" v-model="form.description" type="textarea" placeholder="请输入固件描述">
							</el-input>
						</el-form-item>
					</div>
				</el-scrollbar>
				<submit-reset @getChildNewForm="getNewForm" @getChildResetForm="resetForm('form')">
				</submit-reset>
			</el-form>
		</compoment-drawer>

		<!-- 删除数据确认 -->
		<delete-data :del_data_dialog="del_data_dialog" @getDelData="getDelData" @getCancelDelData="getCancelDelData">
			<el-descriptions class="margin-top" :column="1" size="medium" border>
				<el-descriptions-item>
					<template #label>
						id
					</template>
					{{delData.id}}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						固件名称
					</template>
					{{delData.name}}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						固件描述
					</template>
					{{delData.description}}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						创建日期
					</template>
					{{delData.created_at}}
				</el-descriptions-item>
			</el-descriptions>
		</delete-data>
	</div>
  <!--  mobile  -->
  <div class="mobile-wrap" v-loading="loading" element-loading-text="等下,我正在请求" element-loading-spinner="el-icon-loading">
    <div class="moblie-table-box">
      <el-table :data="data" stripe :header-cell-style="header_cell_style" :max-height="mobile_table_max_height">
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="name" label="固件名称"></el-table-column>
        <el-table-column prop="description" label="固件描述"></el-table-column>
        <el-table-column prop="created_at" label="创建日期"></el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button @click="onFirmwareVersion(scope.row)" type="info" plain size="mini">查看固件版本
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <page-component @pageChanged="onPageChanged"></page-component>
  </div>
</template>

<script>
	import API from '@/api/index.js'
	import mixin from '@/mixin.js'

	export default {
		name: 'Firmware',
		mixins: [mixin],
		data() {
			return {
				// 表单
				drawer_name: '添加固件',
				form_loading: false, // 表单填写时加载
				form: {
					name: '',
					description: ''
				}
			}
		},
		mounted() {
			this.getData();
		},
		methods: {
			async getData() {
				let self = this;
				self.loading = true;
				try {
					let params = {}
					self.addPageInfo(params)
					let res = await API.apks(params.pageNumber, params.pageSize)
					self.loading = false;
					self.onGotPageData(res)
				} catch (err) {
					self.loading = false;
				}
			},
			// 编辑数据
			onEditData(row) {
				let self = this;
				self.drawer_form = true;
				self.form = {
					id: row.id,
					name: row.name,
					description: row.description
				}
			},
			// 确定删除数据
			submitDeleteData(val) {
				let self = this;
				API.delApk(val).then(res => {
					console.log(res)
					self.getData();
					self.delSuccess();
				}).catch(err => {
					self.$message.error(err.response.data.message)
				})
			},
			// 查看固件版本
			onFirmwareVersion(row) {
				localStorage.setItem('apk_id', row.id)
				this.$router.replace({
					path: '/version',
					query: {
						id: row.id
					}
				});
			},
			// 提交表单
			getNewForm() {
				let self = this;
				self.form_loading = true;
				API.apk(self.form).then(res => {
					console.log(res)
					self.submitSuccess();
					self.drawer_form = false;
					self.form_loading = false;
					self.getData();
					self.initForm();
				}).catch(err => {
					self.form_loading = false;
					self.$message.error(err.response.data.message);
				});
			},
			// 初始表单
			initForm() {
				let self = this;
				self.form = {
          name: '',
          description: ''
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.m-top {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 8px;
		background-color: #f0f0f0;

		.m-box {
			margin-right: 10px;
		}
	}

	.table-box {
		margin: 10px 0;
	}

	.m-pagination {
		padding: 20px 0;
		text-align: center;
	}

	.m-form {
		margin: 20px;
	}

	.u-drawer-title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10px;
		font-size: 24px;
	}
</style>
